package com.base.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.common.AjaxResult;
import com.base.common.BaseController;
import com.base.common.TableDataInfo;
import com.base.domain.SysRole;
import com.base.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author gxd
 * @since 2025/6/6
 */
@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色信息")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService roleService;


    @ApiOperation("分页列表-角色列表")
    @PostMapping("/pageList")
    public TableDataInfo pageList(@ApiParam(value = "角色查询条件") SysRole role) {
        startPage(role.getPageNum(), role.getPageSize());
        List<SysRole> roleList = roleService.pageList(role);
        return getDataTable(roleList);
    }

    @ApiOperation("详细信息-根据Id获取角色详情信息")
    @ApiImplicitParam(name = "id", value = "角色Id", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/getById/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return success(roleService.getById(id));
    }

    @ApiOperation("添加或修改-角色信息")
    @PostMapping("/addOrEdit")
    public AjaxResult addOrEdit(@ApiParam(value = "角色实体", required = true) @Valid @RequestBody SysRole role) {
        return success(roleService.addOrEdit(role));
    }

    @ApiOperation("删除-角色信息（支持批量）")
    @ApiImplicitParam(name = "ids", value = "角色Id（多个需逗号分隔）", required = true, allowMultiple = true, dataType = "String", paramType = "path")
    @DeleteMapping("/removeById")
    public AjaxResult removeById(@RequestParam("ids") String ids) {
        Assert.notEmpty(ids, "缺少必要参数：请提供要删除的记录ID");
        return toAjax(roleService.remove(Wrappers.lambdaQuery(SysRole.class).in(SysRole::getId, ids.split(StrPool.COMMA))));
    }
}
